// $codepro.audit.disable expressionValue, handleNumericParsingErrors, largeNumberOfParameters
/**Invoice presenter**/
package edu.gatech.presenters;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

import edu.gatech.datastorage.Invoice;
import edu.gatech.datastorage.TreatmentRecord;
import edu.gatech.utilities.Persistence;

/** Sets and calculates an invoice for a patient
 * 
 * @author Tyler Natoli
 * @version 1.0
 */
public class InvoicePresenter {
	
	/**Treatment record to attach the invoice to**/
	private TreatmentRecord record;
	
	/**Logger*/
	private static final Logger MYLOGGER = Logger.getLogger("My logger");
	
	/**Sets the record from the GUI
	 * 
	 * @param record
	 */
	public InvoicePresenter(TreatmentRecord record){
		this.record = record;
	}
	
	/** Calculates the totals for invoice
	 * 
	 * @param shots
	 * @param xrays
	 * @param checkUps
	 * @param bloodWork
	 * @param surgery
	 */
	public void calculate(String shots, String xrays, String checkUps, 
			String bloodWork, String surgery){
		if(isInteger(shots) && isInteger(xrays) && isInteger(checkUps) && 
				isInteger(bloodWork) && isInteger(surgery)){
			record.setInvoice(new Invoice(Integer.parseInt(shots), 
					Integer.parseInt(xrays), 
					Integer.parseInt(checkUps), Integer.parseInt(bloodWork), 
					Integer.parseInt(surgery)));
			record.getInvoice().calculate();
			Persistence.save();
		}
		else{
			JOptionPane.showMessageDialog(null, "One of the quantity inputs is not" +
					" a valid quantity.");
		}
	}
	
	/** Updates the labels in the invoice GUI
	 * 
	 * @param total
	 * @param shots
	 * @param xrays
	 * @param checkUps
	 * @param bloodWorks
	 * @param surgerys
	 */
	public void updateLabels(JTextField total, JTextField shots, 
			JTextField xrays, JTextField checkUps, 
			JTextField bloodWorks, JTextField surgerys){
		String amount = "$" + record.getInvoice().getAmountOwed();
		String shot = "$" + record.getInvoice().getTotalShots();
		String xray = "$" + record.getInvoice().getTotalXrays();
		String checkUp = "$" + record.getInvoice().getTotalCheckUps();
		String bloodWork = "$" + record.getInvoice().getTotalBloodWork();
		String surgery = "$" + record.getInvoice().getTotalSurgery();
		total.setText(amount);
		shots.setText(shot);
		xrays.setText(xray);
		checkUps.setText(checkUp);
		bloodWorks.setText(bloodWork);
		surgerys.setText(surgery);
	}
	
	/**
	 * Checks if the age input is an integer
	 * @param input
	 * @return boolean if the int is true
	 */
	public boolean isInteger(String input){
		try{
			Integer.parseInt(input); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
			return true;
		}
		catch(NumberFormatException e){
			MYLOGGER.log(Level.SEVERE, "Age not an int", e);
			return false;
		}
	}
	
	/** Overrides toString
	 * @return String
	 */
	public String toString(){
		return "Invoice presenter";
	}
}
